package com.seguriapp.alerts.gwt.client.activity;

import net.customware.gwt.dispatch.client.DispatchAsync;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.seguriapp.alerts.gwt.client.place.AlertsListPlace;
import com.seguriapp.common.gwt.client.AbstractSeguriAppActivity;
import com.seguriapp.common.gwt.client.AbstractSeguriAppPlace;
import com.seguriapp.common.gwt.client.SeguriAppClientSession;
import com.seguriapp.common.gwt.client.SeguriAppDisplay;
import com.seguriapp.entities.Alert;

public class AlertDetailsActivity extends AbstractSeguriAppActivity {

  private final Display display;

  public AlertDetailsActivity(final DispatchAsync dispatchAsync, final EventBus eventBus, final PlaceController placeController, final Display display, final AbstractSeguriAppPlace place, final SeguriAppClientSession clientSession) {
    super(dispatchAsync, eventBus, placeController, place, clientSession);
    display.setPresenter(this);
    this.display = display;
  }

  @Override
  protected void onStart(final AcceptsOneWidget widget, final EventBus eventBus) {
    widget.setWidget(display.asWidget());
    final Object alert = clientSession.get(SeguriAppClientSession.Key.Alert);
    if (alert != null && alert instanceof Alert) {
      getAlertDetails((Alert) alert);
    } else {
      display.setNoDataMessage();
    }
  }

  public void goBackToList() {
    final Place place = new AlertsListPlace("");
    placeController.goTo(place);
  }

  private void getAlertDetails(final Alert alert) {
    display.setData(alert);
  }

  public interface Display extends SeguriAppDisplay {

    /**
     * setea el presenter del activity
     */
    void setPresenter(AlertDetailsActivity presenter);

    void setNoDataMessage();

    void setData(Alert alert);
  }

}
